package cn.iocoder.yudao.module.erp.controller.admin.business.sequence;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.business.sequence.vo.SequencePageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.sequence.vo.SequenceRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.business.sequence.vo.SequenceSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.business.sequence.SequenceDO;
import cn.iocoder.yudao.module.erp.service.business.sequence.SequenceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP 单据编号")
@RestController
@RequestMapping("/erp/sequence")
@Validated
public class SequenceController {

    @Resource
    private SequenceService sequenceService;

    @PostMapping("/create")
    @Operation(summary = "创建单据编号")
    @PreAuthorize("@ss.hasPermission('erp:sequence:create')")
    public CommonResult<Integer> createSequence(@Valid @RequestBody SequenceSaveReqVO createReqVO) {
        return success(sequenceService.createSequence(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新单据编号")
    @PreAuthorize("@ss.hasPermission('erp:sequence:update')")
    public CommonResult<Boolean> updateSequence(@Valid @RequestBody SequenceSaveReqVO updateReqVO) {
        sequenceService.updateSequence(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除单据编号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:sequence:delete')")
    public CommonResult<Boolean> deleteSequence(@RequestParam("id") Integer id) {
        sequenceService.deleteSequence(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得单据编号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:sequence:query')")
    public CommonResult<SequenceRespVO> getSequence(@RequestParam("id") Integer id) {
        SequenceDO sequence = sequenceService.getSequence(id);
        return success(BeanUtils.toBean(sequence, SequenceRespVO.class));
    }

    @GetMapping("/next")
    @Operation(summary = "获得下一个单据编号")
    @PreAuthorize("@ss.hasPermission('erp:sequence:query')")
    public CommonResult<String> nextSequence(@NotEmpty @RequestParam("type") String type) {
        return success(sequenceService.nextSequence(type));
    }
    @GetMapping("/page")
    @Operation(summary = "获得单据编号分页")
    @PreAuthorize("@ss.hasPermission('erp:sequence:query')")
    public CommonResult<PageResult<SequenceRespVO>> getSequencePage(@Valid SequencePageReqVO pageReqVO) {
        PageResult<SequenceDO> pageResult = sequenceService.getSequencePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SequenceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出单据编号 Excel")
    @PreAuthorize("@ss.hasPermission('erp:sequence:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSequenceExcel(@Valid SequencePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SequenceDO> list = sequenceService.getSequencePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "单据编号.xls", "数据", SequenceRespVO.class,
                        BeanUtils.toBean(list, SequenceRespVO.class));
    }

}